#include "SignLogin.h"
#include "ui_SignLogin.h"
#include <QDebug>
#include <QFile>
#include <QHostAddress>
#include <QJsonObject>
#include <QJsonDocument>
#include <QMessageBox>

SignLogin::SignLogin(QWidget *parent) :
    QWidget(parent),
    m_socket(new MyClientSocket(this)),
    ui(new Ui::SignLogin)
{
    ui->setupUi(this);


    //连接服务器
    m_socket->connectToServer("192.168.248.128",8080);

    m_handlemessage = new handlemessage(m_socket);

    //connect(m_socket,&MyClientSocket::dataRecv,this,&SignLogin::handleRecvDataSlot);
    connect(m_handlemessage,&handlemessage::loginSignal,[this](bool flag)
    {
        if(flag)
        {
            QMessageBox::information(this,"登陆状态","登录成功!");
            m_mode = new ChooseMode(m_socket,m_handlemessage);
            qDebug() << "123456" << endl;
            m_mode->show();
            //隐藏当前窗口
            this->hide();
        }
        else {
            QMessageBox::information(this,"登陆状态","登录失败!");
        }
    });


    connect(m_handlemessage,&handlemessage::registerSignal,[this](bool flag)
    {
        if(flag)
        {
            QMessageBox::information(this,"登陆状态","注册成功!");

        }
        else {
            QMessageBox::information(this,"登陆状态","注册成功!");
        }
    });
}

SignLogin::~SignLogin()
{
    delete ui;
}

void SignLogin::on_regisBtn_clicked()
{
    QString username = ui->usernamelineEdit->text();
    QString passwd = ui->passwdlineEdit->text();
    qDebug()<<"注册！" <<username << " "<<passwd;

    //1.创建json对象
    QJsonObject jsonObj;

    //2.设置<key:value>属性
    jsonObj["type"] = REGISTER;
    jsonObj["username"] = username;
    jsonObj["passwd"] = passwd;

    //3.创建jsonDocument对象
    QJsonDocument document(jsonObj);

    m_socket->writeBytes(document.toJson());

}

void SignLogin::on_loginBtn_clicked()
{
    QString username = ui->usernamelineEdit->text();
    QString passwd = ui->passwdlineEdit->text();
    qDebug()<<"登录！"<<username << " "<<passwd;
//    m_mode = new ChooseMode(m_socket,m_handlemessage);

//    m_mode->show();
//    //隐藏当前窗口
//    this->hide();

    //1.创建json对象
    QJsonObject jsonObj;

    //2.设置<key:value>属性
    jsonObj["type"] = LOGIN;
    jsonObj["username"] = username;
    jsonObj["passwd"] = passwd;

    //3.创建jsonDocument对象
    QJsonDocument document(jsonObj);

    m_socket->writeBytes(document.toJson());
}

//void SignLogin::handleRecvDataSlot(const QByteArray &data)
//{
//    qDebug() << "data: "<<data<<endl;

//    //1.解析json字符串
//    QJsonDocument document = QJsonDocument::fromJson(data);

//    //2.将document对象转成json对象
//    QJsonObject jsonObj = document.object();

//    //3.取值
//    int type = jsonObj["type"].toInt();
//    if(type == LOGIN)
//    {
//        QString status = jsonObj["status"].toString();
//        if(status == "ON_SUCCESS")
//        {
//            QMessageBox::information(this,"登陆状态","登录成功!");
//            m_mode = new ChooseMode(m_socket);
//            qDebug() << "123456" << endl;
//            m_mode->show();
//            //隐藏当前窗口
//            this->hide();
//        }
//        else
//        {
//            QString reason = jsonObj["reason"].toString();
//            QMessageBox::warning(this,"登录状态","登录失败:"+reason);
//        }
//    }
//    else if(type == REGISTER)
//    {

//    }
//}
